import {
    parseFrom,
    RuleBuilder,
    Start,
    Timed,
    TimedRuleWordsWrapper,
} from "./rule-builder";
import { createTask } from "./task";

type TaskItem = {
    callback: (...args: any[]) => void;
    args: any[];
    rule: TimedRuleWordsWrapper;
};

class SilkyTimer {
    private taskTable: TaskItem[];
    constructor() {
        this.taskTable = [];
    }

    run(callback: (...args: any[]) => void, ...args: any[]): Start & Timed {
        let builder = new RuleBuilder();

        this.taskTable.push({
            callback: callback,
            args: args,
            rule: builder,
        });

        return builder as Start & Timed;
    }

    start() {
        this.taskTable.forEach((taskItem) => {
            let ruleChain = parseFrom(taskItem.rule.timeRuleWords);
            createTask(taskItem.callback, ruleChain, ...taskItem.args);
        });
    }
}

let silkyTimer = new SilkyTimer();
export { silkyTimer };
